package com.ydy.service.restRecord.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydy.exception.BusinessException;
import com.ydy.exception.DataNotFoundException;
import com.ydy.exception.ValidateException;
import com.ydy.ienum.EnumRestRecord;
import com.ydy.mapper.RestRecordMapper;
import com.ydy.model.RestRecord;
import com.ydy.model.Staff;
import com.ydy.service.restRecord.RestRecordService;
import com.ydy.utils.ValidateUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;
import com.ydy.vo.other.ResultVo;

@Service
@Transactional
public class RestRecordServiceImpl implements RestRecordService {

	private final static Logger log = LoggerFactory.getLogger(RestRecordServiceImpl.class);
	@Autowired
	private RestRecordMapper restRecordMapper;

	@Override
	public PageVo<RestRecord> select(RestRecord restRecord, Integer page, Integer size) {
		PageVo<RestRecord> vo = new PageVo<RestRecord>(page, size);
		Page<RestRecord> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "rest_date desc");
		List<RestRecord> list = restRecordMapper.select(restRecord);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public RestRecord saveOrUpdate(RestRecord restRecord) {
		if (restRecord == null) {
			throw new NullPointerException();
		}
		// 校验数据有效
		Map<String, String> validateInfo = ValidateUtil.validateEntity(restRecord);
		if (!validateInfo.isEmpty()) {
			throw new ValidateException(validateInfo);
		}
		// 新增信息
		if (restRecord.getId() == null) {
			restRecord.setCreateTime(new Date());
			restRecordMapper.insertSelective(restRecord);
			log.info("新增restRecord成功：" + restRecord.getId());
		} else {// 根据id更新信息
			RestRecord temp = restRecordMapper.selectByPrimaryKey(restRecord.getId());
			if (temp == null) {
				log.info("找不到restRecord信息：" + restRecord.getId());
				throw new DataNotFoundException(EnumRestRecord.DATA_NOT_FOUND);
			}
			restRecordMapper.updateByPrimaryKeySelective(restRecord);
			log.info("更新restRecord成功：" + restRecord.getId());
		}
		return restRecord;
	}

	@Override
	public BaseVo delete(Long id, Staff staff) {
		if (id == null) {
			throw new NullPointerException();
		}
		RestRecord temp = restRecordMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到restRecord信息：" + id);
			throw new DataNotFoundException(EnumRestRecord.DATA_NOT_FOUND);
		}
		if (!Objects.equals(temp.getShopId(), staff.getShopId())) {
			throw new BusinessException(EnumRestRecord.RECORD_NOT_BELONG_SHOP);
		}
		restRecordMapper.deleteByPrimaryKey(id);
		log.info("删除restRecord成功：" + id);
		return new ResultVo();
	}

	@Override
	public RestRecord selectById(Long id, Staff staff) {
		if (id == null) {
			throw new NullPointerException();
		}
		RestRecord temp = restRecordMapper.selectByPrimaryKey(id);
		if (temp == null) {
			throw new BusinessException(EnumRestRecord.DATA_NOT_FOUND);
		}
		if (!Objects.equals(temp.getShopId(), staff.getShopId())) {
			throw new BusinessException(EnumRestRecord.RECORD_NOT_BELONG_SHOP);
		}
		return temp;
	}

}
